import webapp2

from lib.toolbox import Render
from lib.toolbox import Sessions


class SignupPage(Render):

  def get(self, url):

      s = Sessions()

      if s.ValidSession(self.request.cookies.get("uid", '')):
        self.redirect("/")
      else:
        self.render("signuppage.html")

  def post(self, url):

      s = Sessions()

      username = self.request.get('username')
      password = self.request.get('password')
      verify = self.request.get('verify')
      email = self.request.get('email')


      uid = s.Signup(username, password, verify, email)
      if uid:
          self.response.headers.add_header('Set-Cookie', 'uid='+ uid +'; Path=/')
          self.redirect("/")
      else:
          self.render("signuppage.html", username = username, password = password, verify = verify, email = email, error="ERROR")
